package com.xianbeiying.app.ckgl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.util.LogUtils;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.adapter.GhSettingAdapter;
import com.xianbeiying.app.ckgl.bean.DevicesBean;
import com.xianbeiying.app.ckgl.bean.GhListBean;
import com.xianbeiying.app.ckgl.bean.UrlBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.GetModel;
import com.xianbeiying.app.ckgl.widght.dapengView.DpView;
import com.xianbeiying.app.ckgl.widght.dapengView.Dpdata;

import java.util.ArrayList;
import java.util.List;

/**
 * 温室设置
 */
public class GhSettingActivity extends BaseActivity implements View.OnClickListener, HttpCallBackUtil, Dpdata {
    private ImageView iv_title_left;
    private TextView tv_title;
    private TextView tv_right_title;
    private ListView listView;
    private GhSettingAdapter ghSettingAdapter;
    //水平滑动的
    private LinearLayout ly_wphorizontal;  //水平滑动的布局
    private View va_line;
    private TextView tv_va_all;
    private GetModel getModel;
    private DpView dpView;
    private ArrayList<GhListBean.VBean.GREENHOUSEBean> greenhouseBeen = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ghseting);
        initView();
    }

    private void initView() {
        MainActivity.fragmentid = 3;
        getModel = new GetModel(this);
        getModel.addResponseListener(this);
        ly_wphorizontal = (LinearLayout) findViewById(R.id.ly_wphorizontal);
        va_line = (View) findViewById(R.id.va_line);
        tv_va_all = (TextView) findViewById(R.id.tv_va_all);
        iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_right_title = (TextView) findViewById(R.id.tv_right_title);
        listView = (ListView) findViewById(R.id.getset_list);
        tv_title.setText("温室设置");
        iv_title_left.setOnClickListener(this);
        tv_right_title.setOnClickListener(this);
        tv_va_all.setOnClickListener(this);
    }

    private void initData() {
        if (MyApplication.devicesBean!= null) {
            devicesBean = MyApplication.devicesBean;
            LogUtils.e(devicesBean.getGREENHOUSE().size() + "==========");
            getData(devicesBean.getGREENHOUSE(), devicesBean.getGREENHOUSE().size(), 0);
            getModel.GETQueryGreenhouse("GET_GREENHOUSE", devicesBean.getGREENHOUSE().get(faId).getGreenhouseId(), true);
        } else {
            //获取大棚数据
            getModel.QUWEYDEVS(true);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            //全部
            case R.id.tv_va_all:
                va_line.setVisibility(View.GONE);
                tv_va_all.setVisibility(View.GONE);
                break;
        }
    }

    //网络返回值
    private DevicesBean devicesBean;
    private GhListBean ghListBean;

    @Override
    public void onCallBackData(String url, Object content) {
        if (url.equals(UrlBean.QUWEYDEVS)) {
            if (content != null) {
                devicesBean = JSON.parseObject(new String((byte[]) content), DevicesBean.class);
                MyApplication.devicesBean = devicesBean;
                getData(devicesBean.getGREENHOUSE(), devicesBean.getGREENHOUSE().size(), 0);
                getModel.GETQueryGreenhouse("GET_GREENHOUSE", devicesBean.getGREENHOUSE().get(faId).getGreenhouseId(), false);
            }
        }
        if (url.equals("查询大棚")) {
            ghListBean = JSON.parseObject(new String((byte[]) content), GhListBean.class);
            if (ghListBean.isR()) {
                greenhouseBeen.clear();
                if(ghListBean!=null&&ghListBean.getV().getGREENHOUSE()!=null&&
                        ghListBean.getV().getGREENHOUSE().getGreenhouseid()!=0&&ghListBean.getV().getGREENHOUSE().getHouseName()!=null){
                greenhouseBeen.add(ghListBean.getV().getGREENHOUSE());
                }
                ghSettingAdapter = new GhSettingAdapter(this, greenhouseBeen);
                listView.setAdapter(ghSettingAdapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent();
                        intent.setClass(GhSettingActivity.this, EdUpGhActivity.class);
                        intent.putExtra("ghListBean", ghListBean);
                        intent.putExtra("ghId", devicesBean.getGREENHOUSE().get(faId).getGreenhouseId());
                        startActivity(intent);
                    }
                });
            }
        }
    }

    private void getData(List<DevicesBean.GREENHOUSEBean> DEVICES, int size, final int id) {
        dpView = new DpView(DEVICES, size, id, ly_wphorizontal, this);
        dpView.setDpdata();
        dpView.addResponseListener(this);
    }

    private int faId;

    @Override
    public void onDpdata(int sid, boolean isChoice) {
        if (isChoice) {
            faId = sid;
            getModel.GETQueryGreenhouse("GET_GREENHOUSE", devicesBean.getGREENHOUSE().get(faId).getGreenhouseId(), true);
        }
    }
}
